package com.unis.XmlNetty;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.unis.db.DBManager;
import com.unis.utils.Config;
import com.unis.utils.utils;


public class XmlUtil {	
	public static boolean writeXml(byte[] bfile,String filename) throws Exception{
		File file = new File(filename);
		if(!file.exists()){
			file.createNewFile();
		}
		FileOutputStream output = new FileOutputStream(file);
		output.write(bfile);
		output.close();
		return false;
	}
	
	public static Document ConfigToXml(String locNode) throws Exception{
		Object obj = new Object();
		//获取本地节点的配置
		String locInfo = "select * from  utm_server_node where c_jdxh = '"+locNode+"'";
		String getRecive = "select * from utm_server_config c ,utm_server_node n where c.c_ydjd = n.c_jdxh and c.c_bdjd = '"+locNode+"' and c.c_cslx = '2'";
		String getSend = "select * from utm_server_config c ,utm_server_node n where c.c_ydjd = n.c_jdxh and c.c_bdjd = '"+locNode+"' and c.c_cslx = '1'";
		ResultSet rs = DBManager.executeQuery(locInfo, obj);
		List<Map<String, Map<Object, String>>>locInforows = utils.ConvertToList(rs);
		DBManager.closeCon(obj);
		
		 rs = DBManager.executeQuery(getRecive, obj);
		 List<Map<String, Map<Object, String>>>reciverows = utils.ConvertToList(rs);
		 DBManager.closeCon(obj);
			
		 rs = DBManager.executeQuery(getSend, obj);
		 List<Map<String, Map<Object, String>>>sendrows = utils.ConvertToList(rs);
		 DBManager.closeCon(obj);
		//建立xml文档
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("config");
		root.addElement("id").addText(locNode);
		
		Element database = root.addElement("database");
		database.addElement("pool").addText(getValue(0,"N_LJC",locInforows));
		database.addElement("url").addText(getValue(0,"C_SJKDZ",locInforows));
		database.addElement("type").addText(getValue(0,"C_SJKLX",locInforows));
		database.addElement("port").addText(getValue(0,"C_SJKDK",locInforows));
		database.addElement("username").addText(getValue(0,"C_SJKYH",locInforows));
		database.addElement("userpassword").addText(getValue(0,"C_SJKMM",locInforows));
		root.addElement("send-count").addText(String.valueOf(sendrows.size()));
		root.addElement("receive-count").addText(String.valueOf(reciverows.size()));
		
		//处理发送配置
		for(int i = 0 ; i < sendrows.size() ; i++){
			Element send = root.addElement("send").addAttribute("id", String.valueOf(i+1));
			send.addElement("type").addText(getValue(i,"C_CSFS",sendrows));
			send.addElement("step").addText(getValue(i,"N_BC",sendrows));
			Element local = send.addElement("local");
			Element sendfile = local.addElement("file");
			sendfile.addElement("path").addText(getValue(i,"C_BDML",sendrows));
			sendfile.addElement("clean-time").addText(getValue(i,"N_QLSC",sendrows));
			Element remote = send.addElement("remote");
			remote.addElement("id").addText(getValue(i,"C_YDJD",sendrows));
			
			Element sendDatabase = remote.addElement("database");
			sendDatabase.addElement("pool").addText(getValue(0,"N_LJC",sendrows));
			sendDatabase.addElement("url").addText(getValue(0,"C_SJKDZ",sendrows));
			sendDatabase.addElement("type").addText(getValue(0,"C_SJKLX",sendrows));
			sendDatabase.addElement("port").addText(getValue(0,"C_SJKDK",sendrows));
			sendDatabase.addElement("username").addText(getValue(0,"C_SJKYH",sendrows));
			sendDatabase.addElement("userpassword").addText(getValue(0,"C_SJKMM",sendrows));
			
			Element tcp = remote.addElement("tcp");
			tcp.addElement("ip").addText(getValue(0,"C_SJKDZ",sendrows));
			tcp.addElement("port").addText(getValue(0,"C_JTDK",sendrows));
		}
		
		for(int i = 0 ; i < reciverows.size() ; i++){
			Element receive = root.addElement("receive").addAttribute("id", String.valueOf(i+1));
			receive.addElement("type").addText(getValue(i,"C_CSFS",reciverows));
			
			Element local = receive.addElement("local");
			Element receivefile = local.addElement("file");
			receivefile.addElement("path").addText(getValue(i,"C_BDML",reciverows));
			receivefile.addElement("scan-time").addText(getValue(i,"N_SMSC",reciverows));
			
			Element tcp = local.addElement("tcp");
			tcp.addElement("port").addText(getValue(0,"C_JTDK",reciverows));
			
			Element remote = receive.addElement("remote");
			remote.addElement("id").addText(getValue(i,"C_YDJD",reciverows));
		}
		Element server = root.addElement("server");
		server.addElement("ip").addText(Config.getConfigValue("server//ip"));
		server.addElement("config-port").addText(Config.getConfigValue("server//config-port"));
		server.addElement("update-port").addText(Config.getConfigValue("server//update-port"));
		server.addElement("control-port").addText(Config.getConfigValue("server//control-port"));
		
		return document;
	}
	
	public static String getValue(int row, String columnName,List<Map<String, Map<Object, String>>> date){
		Object[] o = date.get(row).get(columnName).entrySet().toArray();
		Entry<Object, String> ValueType = (Entry<Object, String>) o[0];
		return ValueType.getKey().toString();
	}
	
	public static void write(Document document) throws IOException {
		XMLWriter writer = null;
		// 美化格式
		OutputFormat format = OutputFormat.createPrettyPrint();
		writer = new XMLWriter(new FileWriter("F://foo.xml"), format);
		writer.write(document);
		writer.close();
		/*
		 * // 缩减格式 format = OutputFormat.createCompactFormat(); writer = new
		 * XMLWriter( System.out, format ); writer.write( document );
		 */
	}
	 
	public static void main(String[] args) throws Exception {
		write(ConfigToXml("100000000"));
	}
}
